<?php
/**
 *
 */
abstract class panel__model__vendors {

	static public function getVendor( $_id_, array $_ = array() ) {

		$R = self::getVendors( array_merge( array("where_id" => $_id_), $_ ) );

		$R = end($R);

		if( !$R && isset_or($_["exception_if_not_exists"]) ) {

			throw new panel__e__not_found(
				"Vendor '{$_id_}' is not found",
				$_["exception_if_not_exists"]
			);

		}	# if

		return $R;

	}	# Function

	static public function getVendors( array $_ = array() ) {

		$Q = p()->db()->select()
		->setDistinct(TRUE)
		->setFrom("runcom.vendors", "v")
		->addSelectEFields("v.id", "VENDOR")
		->addSelect("v.name", "title")
		->addSelectString("", "parent")
		->setOrderBy("v.name");
		;

		if( isset_or($_["where_id"]) ) {

			$Q
			->addWhere("v.id", "=", $_["where_id"])
			->setLimit(1)
			;

		}	# if

		$R = $Q->execute();

		if( isset_or($_["join_devices"]) ) {

			if( !is_array($_["join_devices"]) ) {
				$_["join_devices"] = array(); }

			foreach( $R as $_k => $_v ) {

				$R[$_k]["devices"] = panel__model__devices::getDevices( array_merge( array(

					"where_vendor_id" => $_v["eid"]

				), $_["join_devices"]) );

			}	# foreach

		}	# if

		return $R;

	}	# Function

}	# Class

/* EOF */